﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Web.Mvc;
using BoardPACUtil.Enums;
using System.Xml;

namespace BoardPACUtil.Util
{
    /// <summary>
    ///  <title></title>
    ///  <description></description>
    ///  <copyRight>Copyright (c) 2012</copyRight>
    ///  <company>IronOne Technologies (Pvt)Ltd</company>
    ///  <createdOn>2012-</createdOn>
    ///  <author></author>
    ///  <modification>
    ///     <modifiedBy></modifiedBy>
    ///      <modifiedDate></modifiedDate>
    ///     <description></description>
    ///  </modification>
    /// </summary>
    public class Utility
    {

        #region Member Variables
        #endregion


        #region Properties
        #endregion

        #region Constructors

        /// <summary>
        ///  <description>The default Constructor.</description>
        ///  <param></param>
        ///  <returns>
        ///  </returns>       
        ///  <exception>
        ///  </exception>
        ///  <remarks></remarks>
        /// </summary>
        public Utility()
        {
        }

        #endregion

        #region Functions

        #region PublicFunctions

        public static string GetAction(string Uri)
        {
            string[] segments = Uri.Split('/');
            string output = string.Empty;
            try
            {
                if (segments.Length > 2)
                {
                    output += segments[segments.Length - 2];
                    output += ",";
                    output += segments[segments.Length - 1];
                }
            }
            catch (Exception)
            {

            }
            return output;
        }

        public static string GetParams(string xml)
        {
            string output = string.Empty;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                nsmgr.AddNamespace("s", "http://www.w3.org/2003/05/soap-envelope");
                XmlNode bodyEle = xmlDoc.SelectSingleNode("/s:Envelope/s:Body", nsmgr);
                output = GetString(bodyEle);
            }
            catch (Exception)
            {

            }
            return output;
        }

        private static string GetString(XmlNode node)
        {
            string output = string.Empty;
            foreach (XmlNode item in node.ChildNodes)
            {
                try
                {
                    if (item.HasChildNodes && !string.IsNullOrWhiteSpace(item.FirstChild.InnerXml))
                    {
                        output += GetString(item);
                    }
                    else
                    {
                        output += item.Name + "=" + item.InnerText + "; ";
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
            return output;
        }

        #endregion

        #region PrivateFunctions
        #endregion

        #endregion

    }

    public static class EnumUtility
    {
        //  private static ILog EventLog = LogManager.GetLogger("EventLog");
        //  private static ILog ErrorLog = LogManager.GetLogger("ErrorLog");

        public static IEnumerable<T> EnumToList<T>()
        {
            // EventLog.Info(String.Format(Resources.log_evt_entry_DropdownListGenrator_EnumToList, EventStatus.Entry.ToString()));

            Type enumType = typeof(T);

            if (enumType.BaseType != typeof(Enum))
                throw new ArgumentException("T must be of type System.Enum");

            Array enumValArray = Enum.GetValues(enumType);
            List<T> enumValList = new List<T>(enumValArray.Length);

            foreach (int val in enumValArray)
            {
                enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
            }
            //  EventLog.Info(String.Format(Resources.log_evt_success_DropdownListGenrator_EnumToList, EventStatus.Success.ToString()));
            return enumValList;
        }

        public static string GetEnumDescription(Enum value)
        {
            //     EventLog.Info(String.Format(Resources.log_evt_entry_DropdownListGenrator_GetEnumDescription, EventStatus.Entry.ToString()));

            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);

            // EventLog.Info(String.Format(Resources.log_evt_success_DropdownListGenrator_GetEnumDescription, EventStatus.Success.ToString()));

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        public static IEnumerable<SelectListItem> GetdropDownList()
        {
            //EventLog.Info(String.Format(Resources.log_evt_entry_DropdownListGenrator_GetdropDownList, EventStatus.Entry.ToString()));

            List<SelectListItem> dropdownList = new List<SelectListItem>();
            int enumerationValue = 0;

            foreach (DocumentType state in EnumToList<DocumentType>())
            {
                dropdownList.Add(new SelectListItem
                {
                    Text = GetEnumDescription(state),
                    Value = enumerationValue.ToString(),

                });
                enumerationValue++;
            }
            // EventLog.Info(String.Format(Resources.log_evt_success_DropdownListGenrator_GetdropDownList, EventStatus.Success.ToString()));

            return dropdownList;
        }


        public static IEnumerable<SelectListItem> GetdropDownList(string doctype)
        {
            // EventLog.Info(String.Format(Resources.log_evt_entry_DropdownListGenrator_GetdropDownList, EventStatus.Entry.ToString()));

            List<SelectListItem> dropdownList = new List<SelectListItem>();
            int enumerationValueSelected = 0;
            int enumerationValueNotselected = 0;

            //first put the selected value to dropdownlist
            foreach (DocumentType state in EnumToList<DocumentType>())
            {

                Boolean selectedval = ((state.ToString()).Equals(doctype)) ? true : false;

                if (selectedval)
                {
                    dropdownList.Add(new SelectListItem
                    {
                        Text = GetEnumDescription(state),
                        Value = enumerationValueSelected.ToString(),

                    });
                }
                enumerationValueSelected++;
            }

            //then put the unsilected values dropdownlist
            foreach (DocumentType state in EnumToList<DocumentType>())
            {

                Boolean selectedval = ((state.ToString()).Equals(doctype)) ? true : false;

                if (!selectedval)
                {
                    dropdownList.Add(new SelectListItem
                    {
                        Text = GetEnumDescription(state),
                        Value = enumerationValueNotselected.ToString(),

                    });
                }
                enumerationValueNotselected++;
            }
            //EventLog.Info(String.Format(Resources.log_evt_success_DropdownListGenrator_GetdropDownList, EventStatus.Success.ToString()));
            return dropdownList;
        }


        //dropdown list for paperstatus
        public static IEnumerable<SelectListItem> GetdropDownListPaperStatus(string paperstatus)
        {
            //  EventLog.Info(String.Format(Resources.log_evt_entry_DropdownListGenrator_GetdropDownListPaperStatus, EventStatus.Entry.ToString()));

            List<SelectListItem> dropdownList = new List<SelectListItem>();
            int enumerationValueSelected = 0;
            int enumerationValueNotselected = 0;

            //first put the selected value to dropdownlist
            foreach (PaperStatus state in EnumToList<PaperStatus>())
            {

                Boolean selectedval = ((state.ToString()).Equals(paperstatus)) ? true : false;

                if (selectedval)
                {
                    dropdownList.Add(new SelectListItem
                    {
                        Text = GetEnumDescription(state),
                        Value = enumerationValueSelected.ToString(),

                    });
                }
                enumerationValueSelected++;
            }

            //then put the unsilected values dropdownlist
            foreach (PaperStatus state in EnumToList<PaperStatus>())
            {

                Boolean selectedval = ((state.ToString()).Equals(paperstatus)) ? true : false;

                if (!selectedval)
                {
                    dropdownList.Add(new SelectListItem
                    {
                        Text = GetEnumDescription(state),
                        Value = enumerationValueNotselected.ToString(),

                    });
                }
                enumerationValueNotselected++;
            }
            //    EventLog.Info(String.Format(Resources.log_evt_success_DropdownListGenrator_GetdropDownListPaperStatus, EventStatus.Success.ToString()));

            return dropdownList;
        }

        //dropdown list for paperstatus
        public static IEnumerable<SelectListItem> GetdropDownListInfoOnlyPaperStatus(string infopaper)
        {
            //  EventLog.Info(String.Format(Resources.log_evt_entry_DropdownListGenrator_GetdropDownListInfoOnlyPaperStatus, EventStatus.Entry.ToString()));

            List<SelectListItem> dropdownList = new List<SelectListItem>();


            int enumerationValueSelected = 0;
            int enumerationValueNotselected = 0;

            //first put the selected value to dropdownlist
            foreach (InfoOnlyPaperStatus state in EnumToList<InfoOnlyPaperStatus>())
            {

                Boolean selectedval = ((state.ToString()).Equals(infopaper)) ? true : false;

                if (selectedval)
                {
                    dropdownList.Add(new SelectListItem
                    {
                        Text = GetEnumDescription(state),
                        Value = enumerationValueSelected.ToString(),

                    });
                }
                enumerationValueSelected = (enumerationValueSelected == 0) ? 3 : ++enumerationValueSelected;
            }

            //then put the unselected values dropdownlist
            foreach (InfoOnlyPaperStatus state in EnumToList<InfoOnlyPaperStatus>())
            {

                Boolean selectedval = ((state.ToString()).Equals(infopaper)) ? true : false;

                if (!selectedval)
                {
                    dropdownList.Add(new SelectListItem
                    {
                        Text = GetEnumDescription(state),
                        Value = enumerationValueNotselected.ToString(),

                    });
                }
                enumerationValueNotselected = (enumerationValueNotselected == 0) ? 3 : ++enumerationValueNotselected;
            }
            // EventLog.Info(String.Format(Resources.log_evt_success_DropdownListGenrator_GetdropDownListInfoOnlyPaperStatus, EventStatus.Success.ToString()));

            return dropdownList;
        }

        public static IEnumerable<SelectListItem> GetdropDownListForSaluation()
        {
            List<SelectListItem> dropdownList = new List<SelectListItem>();
            int enumerationValue = 0;

            foreach (SaluationType state in EnumToList<SaluationType>())
            {
                dropdownList.Add(new SelectListItem
                {
                    Text = GetEnumDescription(state),
                });
                enumerationValue++;
            }
            return dropdownList;
        }
    }
}
